/**
* AuthenticateModule.cs
**/
using System;
using System.Xml;

using Yawf.Security;
using Yawf.Security.Entities;
using Yawf.Web.Manager.Entities;
using Yawf.Web.Utils;

namespace Yawf.Web.Managers.Modules {
	public class AuthenticateModule : IModule {
		// Internals
		private IAuthenticationManager manager;

		public IAuthenticationManager Manager { get{return manager;} set{manager = value;} }

		/**
		 * This method will do login of a user or a logout of a user. This module is
		 * called by ModuleModelInvoker when a authenticate tag is found
		 * 
		 * @param elem - a XmlElement object conatinaing Autentication Data
		 * @param dataFetcher - RequestWrapper object to pull out data for parameters to method call
		 * @param dataCollector - ResponseWrapper object where the result will be added
		 * @param user - User currently logged on into the system
		 **/
		public void Execute(XmlElement elem, RequestWrapper dataFetcher, ResponseWrapper dataCollector, IUser user) {
			AuthenticateData aData;
			String login,pwd;
			IUser tmpUser;

			aData = new AuthenticateData(elem);
			if (AuthenticateData.LOGIN_METHOD == aData.Method) {
				login = dataFetcher.GetString(aData.LoginField,"login");
				pwd = dataFetcher.GetString(aData.PasswordField,"This password will not be able to login");
				tmpUser = Manager.Login(login,pwd);

				dataFetcher.SetUser(tmpUser);
			} else if (AuthenticateData.LOGOUT_METHOD == aData.Method) {
				if (user != null) {
					Manager.Logout(user);
					dataFetcher.SetUser(null);
				}
			}
		}

	}
}